#!/bin/bash
IP="
192.168.71.200
192.168.71.201
192.168.71.202
192.168.71.203
192.168.71.204
"
USER="root"
PASSWORD="123456"

install_base_software(){
  apt-get update
  apt-get remove needrestart -y
  apt-get install git sshpass pip iptables -y
  whereis pip && pip config set global.index-url https://mirrors4.tuna.tsinghua.edu.cn/pypi/web/simple
  python3 -m pip install --upgrade pip && pip install ansible
}

push_ssh_key(){
  for host in ${IP}; do
    if [[ ${USER} == 'root' ]];then
      [ ! -f /${USER}/.ssh/id_rsa ] &&\
      ssh-keygen -t rsa -P '' -f /${USER}/.ssh/id_rsa
    else
      [ ! -f /home/${USER}/.ssh/id_rsa ] &&\
      ssh-keygen -t rsa -P '' -f /home/${USER}/.ssh/id_rsa
    fi

    sshpass -p ${PASSWORD} ssh-copy-id -f -o StrictHostKeyChecking=no ${USER}@${host}

    if [ $? -eq 0 ]; then
      echo "$n successed to copy SSH key"
    else
      echo "$n failed to copy SSH key"
    fi
done
}

install_base_software
push_ssh_key
